package com.aigou.admin.aigouspringboot.controller.admin;

import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.Product;
import com.aigou.admin.aigouspringboot.entity.dto.ProductQueryDTO;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.ProductService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author Mr.LiFuXing
 * @Date 2024/12/10 20:22
 * @Title ProductController
 * @Package com.aigou.admin.aigouspringboot.controller
 */
@RestController("adminProduct")
@RequestMapping("/admin/product")
@Slf4j
public class ProductController {

    @Resource
    private ProductService productService;

    /**
     * 分页查询产品列表
     *
     * @param current 当前页码
     * @return 包含产品列表信息的分页结果对象
     */
    @GetMapping
    @LogScope(title = "产品管理模块", content = "查看产品列表信息")
    public Result select(@RequestParam Integer current, ProductQueryDTO productQueryDTO) {

        log.info("查询第{}页的产品信息", current);
        // 分页查询，返回PageInfo（分页插件工具）
        PageInfo<Product> productPageInfo = productService.selectProductListByPage(current,productQueryDTO);
        return Result.success(productPageInfo);
    }

    /**
     * 根据id查询产品详细信息
     *
     * @param id 产品的唯一标识id
     * @return 对应的产品对象，若查询成功则包含产品详细信息，否则返回相应错误提示
     */
    @GetMapping("/{id}")
    @LogScope(title = "产品管理模块", content = "根据id查询产品信息")
    public Result detail(@PathVariable String id) {

        Product product = productService.selectById(id);
        return Result.success(product);
    }

    /**
     * 根据id删除产品信息
     *
     * @param id 产品的唯一标识id
     * @return 若删除操作成功返回成功结果，否则返回失败结果
     */
    @DeleteMapping("/{id}")
    @CacheEvict(value = {"importer","costume","beautyMakeup","book"},allEntries = true)
    @LogScope(title = "产品管理模块", content = "根据id删除产品信息")
    public Result delete(@PathVariable String id) {

        int i = productService.deleteById(id);
        return i > 0? Result.success() : Result.error();
    }

    /**
     * 编辑产品信息
     *
     * @param product 包含修改后产品数据的对象
     * @return 若编辑操作成功返回成功结果，否则返回失败结果
     */
    @PutMapping
    @CacheEvict(value = {"importer","costume","beautyMakeup","book"},allEntries = true)
    @LogScope(title = "产品管理模块", content = "编辑产品信息")
    public Result edit(@RequestBody Product product) {

        int i = productService.edit(product);
        return i > 0? Result.success() : Result.error();
    }

    /**
     * 添加产品信息
     *
     * @param product 要添加的产品对象
     * @return 若添加操作成功返回成功结果，否则返回失败结果
     */
    @PostMapping("/save")
    @LogScope(title = "产品管理模块", content = "添加产品信息")
    @CacheEvict(value = {"importer","costume","beautyMakeup","book"},allEntries = true)
    public Result save(@RequestBody Product product) {

        product.setId(RandomUtil.getRandomNumber());
        product.setCreateTime(new Date());
        int i = productService.save(product);
        return i > 0? Result.success() : Result.error();
    }
}